package dome7;

/**
 *
 *  深拷贝：
 *  实现拷贝之前先让类具备拷贝的功能-->实现Cloneable接口，重写Clone方法
 */


class Money implements Cloneable{
    public double money = 12.56;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Student implements Cloneable{
    public String name;
    public Money m = new Money();//组合

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {  //深拷贝
        //拷贝Student对象
        Student student = (Student) super.clone();
        //拷贝Student对象中的Money对象
        student.m = (Money) this.m.clone();
        return student;
    }
}


public class Test {

    public static void main(String[] args) throws CloneNotSupportedException{
        Student student1 = new Student();
        Student student2 = (Student) student1.clone();
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
        System.out.println("=================");
        student2.m.money = 99;
        System.out.println(student1.m.money); //
        System.out.println(student2.m.money);
    }
}
